package com.xzp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class GirlController {

    @Autowired
    private GirlRepository girlRepository;

    @Autowired
    private GirlService girlService;

    @GetMapping("/girls")
    private List<Girl> girlList(){
        return girlRepository.findAll();
    }

//    @PostMapping("/girls")
//    private Girl girlAdd(@RequestBody Girl girl){  //传递json对象
//
//        return girlRepository.save(girl);
//    }

    @PostMapping("/girls")
    private Girl girlAdd(@RequestParam("cupSize") String cupSize,
                         @RequestParam("age") Integer age){
        Girl girl = new Girl();
        girl.setAge(age);
        girl.setCupSize(cupSize);
        return girlRepository.save(girl);
    }

    @GetMapping(value = "/girls/{id}")
    private Girl girlFindOne(@PathVariable("id") Integer id){
//        return girlRepository.findOne(id); 老版本写法
        return girlRepository.findById(id).orElse(null);  //SpringBoot 2.0+ 新写法
    }

    //更新信息
    @PutMapping(value = "/girls/{id}")
    private Girl girlUpdate(@PathVariable("id") Integer id,
                            @RequestParam("cupSize") String cupSize,
                            @RequestParam("age") Integer age){

        Girl girl = new Girl();
        girl.setId(id);
        girl.setCupSize(cupSize);
        girl.setAge(age);

        return girlRepository.save(girl);
    }

    @DeleteMapping(value = "/girls/{id}")
    private void girlDelete(@PathVariable("id") Integer id,
                            @RequestParam("cupSize") String cupSize,
                            @RequestParam("age") Integer age){
        System.out.println(cupSize+age); //测试接受参数
        girlRepository.deleteById(id);
    }

    @GetMapping(value = "/girls/age")
    private List<Girl> findByAge(@RequestParam("age") Integer age){
        return girlRepository.findByAge(age);
    }

    /**
     * 插入两个女生
     */
    @PostMapping(value = "/girls/two")
    private void girlTwo(){
        girlService.insertTwo();
    }
}
